﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using QASPRibbon.Commands;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// SplitButton, which is contains most recent clicked of it's inner buttons as the default button.
    /// So, MRUSplitButton is not a button itself, and don't have any visual elements
    /// </summary>
    public class RibbonMRUSplitButton : RibbonVisualControl, IRibbonMenu
    {
        #region JavaScript

        /// <summary>
        /// Javascript code that executes when a MRUSplitButton is activated.
        /// </summary>
        public string OnClientActivate { get; set; }

        /// <summary>
        /// Javascript code that executes when a MRUSplitButton's Click event is raised.
        /// </summary>
        public string OnClientClick { get; set; }

        /// <summary>
        /// Javascript code that executes when a MRUSplitButton's enable/disable state is checked.
        /// </summary>
        public string OnClientStateCheck { get; set; }

        #endregion

        #region Properties

        /// <summary>
        /// An alternative text that is used for the MRUSplitButton.
        /// </summary>
        public string Alt
        {
            get { return _alt; }
            set { _alt = value; }
        }

        /// <summary>
        /// Initial button ID, this must be ID of one of inner controls. Required.
        /// </summary>
        public string InitialItem
        {
            get { return _initialItem; }
            set { _initialItem = value; }
        }

        /// <summary>
        /// Child menu sections of the menu
        /// </summary>
        [Obsolete("Use property Controls instead Sections")]
        public IEnumerable<RibbonMenuSection> Sections { get { return Controls; } set { Controls = value; } }

        /// <summary>
        /// Child controls of the menu
        /// </summary>
        public IEnumerable<RibbonMenuSection> Controls { get; set; }

        #endregion

        #region Internal

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute("TemplateAlias", TemplateAlias));
            element.Add(new XAttribute("CacheMenuVersions", "true"));
            element.Add(new XAttribute("PopulateDynamically", "false"));
            element.Add(new XAttribute("PopulateOnlyOnce", "false"));
            element.Add(new XAttribute("CommandQuery", FullId + "CommandQuery"));
            element.Add(new XAttribute("Command", FullId + "Command"));
            element.Add(new XAttribute("InitialItem", FullId + ".Menu." + Controls.First().Id + ".Controls." + InitialItem));
            var menuElement = new XElement("Menu", new XAttribute("Id", FullId + ".Menu"));
            var sectionIndex = 0;
            foreach (var section in Controls)
            {
                sectionIndex++;
                section.Sequence = sectionIndex;
                section.NameSpace = FullId + ".Menu";
                menuElement.Add(section.AsXElement());
            }
            element.Add(menuElement);
            return element;
        }

        internal override string XmlTag
        {
            get { return Schema.MRUSplitButton; }
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>
                              {
                                  new RibbonCommand(FullId, "CheckState",
                                                    string.Format("{0}; {1}; return {2}",
                                                                  OnClientActivate ?? string.Empty, OnClientStateCheck,
                                                                  Enabled ? "true" : "false")),
                                  new RibbonCommand(FullId, Schema.Command, string.Format("{0}; {1}", OnClientClick ?? string.Empty, "handleCommand$NAMESPACE$(properties.CommandValueId);")),
                                  new RibbonCommand(FullId, Schema.QueryCommand, string.Format("{0}; {1}; return {2};", OnClientActivate ?? string.Empty, OnClientStateCheck, Enabled ? "true" : "false"))
                              };
                return res;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMRUSplitButton.
        /// </summary>
        public RibbonMRUSplitButton() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMRUSplitButton.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonMRUSplitButton(string id, string title) : base(id, title) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonMRUSplitButton.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonMRUSplitButton(string id) : base(id) { }

        #endregion
        
        #region Private fields

        private string _alt = string.Empty;
        private string _initialItem = string.Empty;

        #endregion
    }
}
